package com.controller;

import com.dao.Person1Dao;
import com.pojo.Student;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class StudentHandler {
    @Autowired
    private Person1Dao studentDao;

    @GetMapping("/findAll/{page}/{size}")
    @ApiOperation("查询所有的神奇宝贝信息")
    public Page<Student> findAllStudents(@PathVariable("page")Integer page, @PathVariable("size")Integer size){
        Pageable pageable = PageRequest.of(page-1, size);
        return studentDao.findAll(pageable);
    }
    @PostMapping("/save")
    @ApiOperation("添加一个对象信息")
//    public String save(@ApiParam("用户对象") @ModelAttribute Student student){ @ApiParam("用户对象")会不起作用
    public String save(@ApiParam("用户对象") @RequestBody Student student){
        System.out.println(student);
        Student student1 = studentDao.save(student);
        if (student1!=null){
            return "success";
        }else{
            return "error";
        }
    }
    @GetMapping("/findById/{id}")
    @ApiOperation("通过ID查询单个神奇宝贝信息")
    public Student fingById(@PathVariable("id") Integer id){
        return studentDao.findById(id).get();
    }

    @PostMapping("/update")
    @ApiOperation("修改神奇宝贝的信息")

    public String update(@ApiParam("对象信息")@RequestBody Student student){
//    public String update(@RequestBody Student student){
        System.out.println(student);
        Student student1 = studentDao.save(student);
        if (student1!=null){
            return "success";
        }else{
            return "error";
        }
    }
    @DeleteMapping("/deleteById/{id}")
    @ApiOperation("通过ID删除一个神奇宝贝的信息")
    public void deleteById(@PathVariable("id")Integer id){
        studentDao.deleteById(id);
    }
}
